package com.foodplus.www.view.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foodplus.www.adapter.basket.BasketAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.basket.BasketBean;
import com.foodplus.www.bean.basket.BasketGoodNumBean;
import com.foodplus.www.bean.basket.GoodsBean;
import com.foodplus.www.foodplus.MyApp;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.BasketGoodNumPresenter;
import com.foodplus.www.presenter.BasketPresenter;
import com.foodplus.www.view.impl.BasketView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class BasketFragment extends Fragment implements BasketView {


    @BindView(R.id.tv_basket)
    TextView tvBasket;
    @BindView(R.id.tv_goods)
    TextView tvGoods;
    @BindView(R.id.tv_look)
    Button tvLook;
    @BindView(R.id.rl_no_goods)
    RelativeLayout rlNoGoods;
    @BindView(R.id.rec_view_basket)
    RecyclerView recViewBasket;

    BasketAdapter adapter;
    @BindView(R.id.tv_price_total)
    TextView tvPriceTotal;
    @BindView(R.id.btn_pay)
    Button btnPay;
    @BindView(R.id.ll_price_total)
    LinearLayout llPriceTotal;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.cb_sel_all)
    CheckBox cbSelAll;


    private BasketBean data;

    public BasketFragment() {
    }

    public static BasketFragment newInstance() {

        Bundle args = new Bundle();

        BasketFragment fragment = new BasketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_basket, container, false);
        ButterKnife.bind(this, view);

        initView();

        return view;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            //每次进入都要加载,故数据加载直接写在onHiddenChanged里面
            if (MyApp.getInstance().isLoggedIn()) {
                initData();
            } else {
                recViewBasket.setVisibility(View.GONE);
                llPriceTotal.setVisibility(View.GONE);
                tvEdit.setVisibility(View.GONE);
                rlNoGoods.setVisibility(View.VISIBLE);
            }
            refreshEditState();
        }

    }

    private void initData() {
        new BasketPresenter(this).getBasketGoods(ParamManager.getInstance().getBasketParams());
    }

    private void initView() {
        adapter = new BasketAdapter(data);
        recViewBasket.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setChangeNumListener(new BasketAdapter.OnChangeNumListener() {
            @Override
            public void onAddNumListener(GoodsBean item) {
                if (Integer.parseInt(item.getGoods_storage()) > Integer.parseInt(item.getGoods_num())) {
                    postChangeNumData(Integer.parseInt(item.getGoods_num()) + 1, item.getCart_id());
                } else {
                    Toast.makeText(getActivity(), "已达数量上限", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onMinusNumListener(GoodsBean item) {
                if (Integer.parseInt(item.getGoods_num()) != 1) {
                    postChangeNumData(Integer.parseInt(item.getGoods_num()) - 1, item.getCart_id());
                } else {
                    Toast.makeText(getActivity(), "不能再减少了", Toast.LENGTH_SHORT).show();
                }
            }
        });
        recViewBasket.setAdapter(adapter);
    }

    //当购物车的单个商品购买数量改变时调用
    private void postChangeNumData(int num, String cartId) {
        new BasketGoodNumPresenter(this).getBasketGoodNum(ParamManager.getInstance().getChangeNumMap(num, cartId), cartId);
    }

    @Override
    public void onSuccess(BasketBean basketBean) {
        if (basketBean == null || basketBean.getDatas().getCart_list() == null || basketBean.getDatas().getCart_list().size() == 0) {
            recViewBasket.setVisibility(View.GONE);
            llPriceTotal.setVisibility(View.GONE);
            tvEdit.setVisibility(View.GONE);
        } else {
            recViewBasket.setVisibility(View.VISIBLE);
            llPriceTotal.setVisibility(View.VISIBLE);
            tvEdit.setVisibility(View.VISIBLE);
            rlNoGoods.setVisibility(View.GONE);
            this.data =basketBean;
            adapter.refreshData(data);
        }
        refreshEditState();
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    @OnClick(R.id.tv_edit)
    public void click(View view) {
        switch (view.getId()) {

        }
        MyApp.getInstance().setBasketEditting(!MyApp.getInstance().isBasketEditting());
        refreshEditState();
        adapter.notifyDataSetChanged();
    }

    private void refreshEditState() {
        if (MyApp.getInstance().isBasketEditting()) {
            tvEdit.setText("完成");
        } else {
            tvEdit.setText("编辑");
        }
    }

    @Override
    public void onChangeNum(BasketGoodNumBean bean) {

        if (bean.getCode() == 200) {
            String cartId = bean.getDatas().getCartId();
            for (int i = 0; i < data.getDatas().getCart_list().size(); i++) {
                for (int j = 0; j < data.getDatas().getCart_list().get(i).getGoods().size(); j++) {
                    if (data.getDatas().getCart_list().get(i).getGoods().get(j).getCart_id().equals(bean.getDatas().getCartId() + "")) {
                        data.getDatas().getCart_list().get(i).getGoods().get(j).setGoods_num(bean.getDatas().getQuantity() + "");
                        adapter.refreshData(data);
                        return;
                    }
                }
            }

        }
        Toast.makeText(getContext(), "服务器返回失败", Toast.LENGTH_SHORT).show();

    }
}
